package com.cy.springcloudalibaba.services;

import com.cy.springcloudalibaba.models.Product;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

/**
 * @Description: 商品
 * @ClassName: ProductService
 * @Author: chenyu
 * @Date: 2025/2/6 10:52
 */
@LocalTCC
public interface ProductService {
    /**
     *  减库存
     *  定义两阶段提交
     *  name=reduceStock为一阶段try方法
     *  commitMethod = commitTcc 为二阶段提交方法
     *  rollbackMethod = cancelTcc 为二阶段取消方法
     *  BusinessActionContextParameter注解 可传递参数到二阶段方法
     * @author chenyu
     * @param productId 商品id
     * @param amount 扣减数量
     * @return com.cy.springcloudalibaba.models.Product
     * @date 2025/2/7 10:17
     */
    @TwoPhaseBusinessAction(name = "reduceStock", commitMethod = "commitTcc", rollbackMethod = "cancelTcc")
    Product reduceStock(@BusinessActionContextParameter(paramName = "productId") Integer productId,
                        @BusinessActionContextParameter(paramName = "amount") Integer amount) throws Exception;

    /**
     *  确认方法，可以另命名，但要保证与commitMethod值一致
     * @author chenyu
     * @param context
     * @return boolean
     * @date 2025/2/7 10:23
     */
    boolean commitTcc(BusinessActionContext context);

    /**
     *  二阶段取消方法
     * @author chenyu
     * @param context
     * @return boolean
     * @date 2025/2/7 10:23
     */
    boolean cancelTcc(BusinessActionContext context);
}
